package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Gathering;
import cn.wolfcode.crm.query.GatheringQueryForObject;
import cn.wolfcode.crm.service.IGatheringService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Heroes on 2018/7/11.
 */
@Controller
@RequestMapping("gathering")
public class GatheringController {
    @Autowired
    private IGatheringService gatheringService;

    @PermissionName("收款记录首页")
    @RequiresPermissions("gathering:index")
    @RequestMapping("index")
    public String index() {
        return "gathering/list";
    }

    @PermissionName("收款记录列表")
    @RequiresPermissions("gathering:list")
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(GatheringQueryForObject qo) {
        return gatheringService.query(qo);
    }

    @RequestMapping("edit")
    @ResponseBody
    public JsonResult edit(Gathering record) {
        try {
            gatheringService.updateByPrimaryKey(record);
        } catch (Exception e) {
            e.printStackTrace();
            new JsonResult("操作失败");
        }
        return new JsonResult();
    }

    @PermissionName("收款记录添加")
    @RequiresPermissions("gathering:save")
    @RequestMapping("save")
    @ResponseBody
    public JsonResult save(Gathering record) {
        try {
            record.setState(0);
            gatheringService.insert(record);
        } catch (Exception e) {
            e.printStackTrace();
            new JsonResult("操作失败");
        }
        return new JsonResult();
    }

    /**
     * 修改状态
     *
     * @param feeId
     * @param stuId
     * @return
     */
    @PermissionName("收款记录审核")
    @RequiresPermissions("gathering:changeState")
    @RequestMapping("changeState")
    @ResponseBody
    public JsonResult changeState(Long feeId, Long stuId) {
        try {
            gatheringService.changeState(feeId, stuId);
        } catch (Exception e) {
            e.printStackTrace();
            new JsonResult("审核失败");
        }
        return new JsonResult();
    }

    //导出excel
    @RequestMapping("exportExcel")
    public void exportExcel(HttpServletResponse response) throws Exception {
        //导出
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
        String fileName = sdf.format(new Date()) + ".xls";
        response.setContentType("application/x-excel");
        response.setCharacterEncoding("UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename="
                + fileName);
        List<Gathering> list = new ArrayList<>();
        List<Gathering> officialstus = gatheringService.selectAll();
        for (Gathering officialstu : officialstus) {
            list.add(officialstu);
        }
        //1.创建工作薄
        WritableWorkbook workbook = Workbook.createWorkbook(response.getOutputStream());
        //2.创建页签
        WritableSheet sheet = workbook.createSheet("员工列表", 0);
        //3.创建单元格
        String[] header = new String[]{"学员名字", "班級", "收款人", "收款时间", "收款金额", "支付方式", "未缴学费", "营销人员", "单据号", "备注", "状态"};
        Label label = null;
        for (int i = 0; i < header.length; i++) {
            label = new Label(i, 0, header[i]);
            //4.添加到页签中
            sheet.addCell(label);
        }
        //迭代数据
        for (int i = 1; i < list.size() + 1; i++) {
            if(list.get(i - 1).getStudent()==null){
                label = new Label(0, i, "");
                sheet.addCell(label);
            }else
            {
                label = new Label(0, i, list.get(i - 1).getStudent().getName());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getIttclass() == null) {
                label = new Label(1, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(1, i, list.get(i - 1).getIttclass().getName());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getPayee().getUsername() == null) {
                label = new Label(2, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(2, i, list.get(i - 1).getPayee().getUsername());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getReceiptTime() == null) {
                label = new Label(3, i, "");
                sheet.addCell(label);
            } else {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(list.get(i - 1).getReceiptTime());
                label = new Label(3, i, time);
                sheet.addCell(label);
            }
            if (list.get(i - 1).getTotalFee() == null) {
                label = new Label(4, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(4, i, list.get(i - 1).getTotalFee().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getPayment() == null) {
                label = new Label(5, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(5, i, list.get(i - 1).getPayment().getName());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getNoPay() == null) {
                label = new Label(6, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(6, i, list.get(i - 1).getNoPay().setScale(2, BigDecimal.ROUND_HALF_UP).toString());
                sheet.addCell(label);
            }
            if (list.get(i - 1).getMarketer() == null) {
                label = new Label(7, i, "");
                sheet.addCell(label);
            } else {
                label = new Label(7, i, list.get(i - 1).getMarketer().getUsername());
                sheet.addCell(label);
            }
            label = new Label(8, i, list.get(i - 1).getNo());
            sheet.addCell(label);
            label = new Label(9, i, list.get(i - 1).getRemark());
            sheet.addCell(label);
            label = new Label(10, i, list.get(i - 1).getState().toString());
            sheet.addCell(label);
        }
        //5.写出数据
        workbook.write();
        workbook.close();
    }
}
